package com.ruoyi.api.yh.blood;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import com.ruoyi.api.yh.version.ApiVersion;
import com.ruoyi.common.constant.RedisConstant;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.yh.api.R;
import com.ruoyi.common.yh.api.aop.LoginRequired;
import com.ruoyi.common.yh.api.dtovo.blood.BloodInfoVO;
import com.ruoyi.yh.domain.YiBloodInfo;
import com.ruoyi.yh.service.IYiBloodInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.dozer.Mapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;
import java.util.concurrent.TimeUnit;

@ApiSupport(order = 0)
@Api(tags="姓氏管理")
@RestController
@ApiVersion(1)
@RequestMapping("/{version}/app-api/bloodInfo")
public class YiBloodInfoApiController extends BaseController{
    @Autowired
    private Mapper dozerMapper;
    @Autowired
    private IYiBloodInfoService yiBloodInfoService;
    /**
     *
     * cmm .21
     */

    @ApiOperation("获取百家姓列表")
    @ApiOperationSupport(author = "cmm",order = 0)
    @RequestMapping(value = "/getBloddInfoList",method = RequestMethod.GET)
    public R<Map<String,List<List<BloodInfoVO>>>> getBloddInfoList()
    {
        YiBloodInfo yiBloodInfoquery=new YiBloodInfo();
        List<YiBloodInfo> list = yiBloodInfoService.selectYiBloodInfoList(yiBloodInfoquery);


        Collections.sort(list, new Comparator<YiBloodInfo>() {
            @Override
            public int compare(YiBloodInfo o1, YiBloodInfo o2) {
                return o2.getBloodNo().compareTo(o1.getBloodNo());//降序
            }
        });

        List<List<BloodInfoVO>> pareBloodInfoVOListAll=new ArrayList<>();
        List<List<BloodInfoVO>> pareBloodInfoVOList=new ArrayList<>();
        List<List<BloodInfoVO>> pareBloodInfoVOList_A=new ArrayList<>();
        List<List<BloodInfoVO>> pareBloodInfoVOList_B=new ArrayList<>();
        List<List<BloodInfoVO>> pareBloodInfoVOList_O=new ArrayList<>();
        List<BloodInfoVO> bloodInfoVOListAll=new ArrayList<>();
        List<BloodInfoVO> bloodInfoVOList=null;
        List<BloodInfoVO> bloodInfoVOList_A=null;
        List<BloodInfoVO> bloodInfoVOList_B=null;
        List<BloodInfoVO> bloodInfoVOList_O=null;
        int i=0,b=0,a=0,o=0;
        for(YiBloodInfo yiBloodInfo:list){
            BloodInfoVO bloodInfoVO=dozerMapper.map(yiBloodInfo, BloodInfoVO.class);
            if(yiBloodInfo.getBloodType().equals("AB")){
                if(i==0||i==1){
                    if(i==0){
                        bloodInfoVOList=new ArrayList<>();
                    }
                    bloodInfoVOList.add(bloodInfoVO);
                    i++;
                }else if(i==2){
                    bloodInfoVOList.add(bloodInfoVO);
                    pareBloodInfoVOList.add(bloodInfoVOList);
                    i=0;
                }
            }else
            if(yiBloodInfo.getBloodType().equals("A")){
                if(a==0||a==1){
                    if(a==0){
                        bloodInfoVOList_A=new ArrayList<>();
                    }
                    bloodInfoVOList_A.add(bloodInfoVO);
                    a++;
                }else if(a==2){
                    bloodInfoVOList_A.add(bloodInfoVO);
                    pareBloodInfoVOList_A.add(bloodInfoVOList_A);
                    a=0;
                }
            }else
            if(yiBloodInfo.getBloodType().equals("B")){
                if(b==0||b==1){
                    if(b==0){
                        bloodInfoVOList_B=new ArrayList<>();
                    }
                    bloodInfoVOList_B.add(bloodInfoVO);
                    b++;
                }else if(b==2){
                    bloodInfoVOList_B.add(bloodInfoVO);
                    pareBloodInfoVOList_B.add(bloodInfoVOList_B);
                    b=0;
                }
            }else
            if(yiBloodInfo.getBloodType().equals("O")){
                if(o==0||o==1){
                    if(o==0){
                        bloodInfoVOList_O=new ArrayList<>();
                    }
                    bloodInfoVOList_O.add(bloodInfoVO);
                    o++;
                }else if(o==2){
                    bloodInfoVOList_O.add(bloodInfoVO);
                    pareBloodInfoVOList_O.add(bloodInfoVOList_O);
                    o=0;
                }
            }
            bloodInfoVOListAll.add(bloodInfoVO);

        }
        pareBloodInfoVOListAll.add(bloodInfoVOListAll);//查询使用
        if(i!=0){
            pareBloodInfoVOList.add(bloodInfoVOList);
        }
        if(a!=0){
            pareBloodInfoVOList_A.add(bloodInfoVOList_A);
        }
        if(b!=0){
            pareBloodInfoVOList_B.add(bloodInfoVOList_B);
        }
        if(o!=0){
            pareBloodInfoVOList_O.add(bloodInfoVOList_O);
        }
        Map<String,List<List<BloodInfoVO>>> bloodInfoVOMap=new HashMap<>();
        bloodInfoVOMap.put("AB",pareBloodInfoVOList);
        bloodInfoVOMap.put("A",pareBloodInfoVOList_A);
        bloodInfoVOMap.put("B",pareBloodInfoVOList_B);
        bloodInfoVOMap.put("O",pareBloodInfoVOList_O);
        bloodInfoVOMap.put("All",pareBloodInfoVOListAll);
        return R.ok(bloodInfoVOMap,"获取百家姓列表");
    }

    /**
     *
     * cmm .21
     */

    @ApiOperation("获取百家姓列表")
    @ApiOperationSupport(author = "cmm",order = 0)
    @RequestMapping(value = "/getBloddInfoListTwo",method = RequestMethod.GET)
    public R<Map<String,List<BloodInfoVO>>> getBloddInfoListTwo()
    {
        YiBloodInfo yiBloodInfoquery=new YiBloodInfo();
        List<YiBloodInfo> list = yiBloodInfoService.selectYiBloodInfoList(yiBloodInfoquery);


        Collections.sort(list, new Comparator<YiBloodInfo>() {
            @Override
            public int compare(YiBloodInfo o1, YiBloodInfo o2) {
                return o1.getBloodOrder().compareTo(o2.getBloodOrder());//升序
            }
        });
        List<BloodInfoVO> bloodInfoVOList=new ArrayList<>();
        List<BloodInfoVO> bloodInfoVOList_A=new ArrayList<>();
        List<BloodInfoVO> bloodInfoVOList_B=new ArrayList<>();
        List<BloodInfoVO> bloodInfoVOList_O=new ArrayList<>();
        for(YiBloodInfo yiBloodInfo:list){
            BloodInfoVO bloodInfoVO=dozerMapper.map(yiBloodInfo, BloodInfoVO.class);
            if(yiBloodInfo.getBloodType().equals("AB")){
                bloodInfoVOList.add(bloodInfoVO);
            }else
            if(yiBloodInfo.getBloodType().equals("A")){
                bloodInfoVOList_A.add(bloodInfoVO);
            }else
            if(yiBloodInfo.getBloodType().equals("B")){
                bloodInfoVOList_B.add(bloodInfoVO);
            }else
            if(yiBloodInfo.getBloodType().equals("O")){
                bloodInfoVOList_O.add(bloodInfoVO);
            }
        }

        Map<String,List<BloodInfoVO>> bloodInfoVOMap=new HashMap<>();
        bloodInfoVOMap.put("AB",bloodInfoVOList);
        bloodInfoVOMap.put("A",bloodInfoVOList_A);
        bloodInfoVOMap.put("B",bloodInfoVOList_B);
        bloodInfoVOMap.put("O",bloodInfoVOList_O);
        return R.ok(bloodInfoVOMap,"获取百家姓列表");
    }

    /**
     *
     * cmm .21
     */
    @LoginRequired
    @ApiOperation("获取百家姓列表-下拉框使用")
    @ApiOperationSupport(author = "cmm",order = 0)
    @RequestMapping(value = "/getBloddInfoListThree",method = RequestMethod.GET)
    public R<List<BloodInfoVO>> getBloddInfoListThree()
    {
        YiBloodInfo yiBloodInfoquery=new YiBloodInfo();
        List<YiBloodInfo> list = yiBloodInfoService.selectYiBloodInfoList(yiBloodInfoquery);

        Collections.sort(list, new Comparator<YiBloodInfo>() {
            @Override
            public int compare(YiBloodInfo o1, YiBloodInfo o2) {
                return o1.getBloodOrder().compareTo(o2.getBloodOrder());//升序
            }
        });
        List<BloodInfoVO> bloodInfoVOList=new ArrayList<>();

        for(YiBloodInfo yiBloodInfo:list){
            BloodInfoVO bloodInfoVO=dozerMapper.map(yiBloodInfo, BloodInfoVO.class);
            bloodInfoVOList.add(bloodInfoVO);
        }

        return R.ok(bloodInfoVOList,"获取百家姓列表");
    }
}
